package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.to.MemberVo;
import com.atguigu.gulimall.member.exception.PhoneAlreadyExistException;
import com.atguigu.gulimall.member.exception.UsernameAlreadyExistException;
import com.atguigu.gulimall.member.feign.MemberCouponService;
import com.atguigu.gulimall.member.vo.GiteeOAuth2RespVo;
import com.atguigu.gulimall.member.vo.RegisterVo;
import com.atguigu.gulimall.member.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author shujun
 * @email 961624576@qq.com
 * @date 2024-01-14 18:14:44
 */
@Slf4j
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    @Autowired
    private MemberCouponService memberCouponService;

    @RequestMapping("/coupon")
    public R getMemberCoupon() {
        R response = memberCouponService.list(new HashMap<String, Object>());
        return R.ok().put("page",response.get("page"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        try {
            memberService.register(registerVo);
        } catch (UsernameAlreadyExistException e) {
            return R.error(BizCodeEnum.USER_ALREADY_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_ALREADY_EXIST_EXCEPTION.getMsg());
        } catch (PhoneAlreadyExistException e) {
            return R.error(BizCodeEnum.PHONE_ALREADY_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_ALREADY_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo){
        MemberEntity member = memberService.login(userLoginVo);
        if (member == null){
            return R.error(BizCodeEnum.ACCOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),
                    BizCodeEnum.ACCOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }

        return R.ok().put("data",member);
    }

    @PostMapping("/auth2Login")
    public R auth2Login(@RequestBody GiteeOAuth2RespVo giteeOAuth2RespVo){
        MemberEntity memberEntity = null;
        try {
            memberEntity = memberService.auth2Login(giteeOAuth2RespVo);
        }catch (Exception e){
            log.error(e.getMessage());
            return R.error();
        }

        return R.ok().put("data",memberEntity);
    }
}
